//
//  FollowVC.m
//  BiliBiliDemo
//
//  Created by XDH on 16/4/12.
//  Copyright © 2016年 XDH. All rights reserved.
//

#import "FollowVC.h"

#import "FZQChaseProgramViewController.h"

#import "FZQDynamicViewController.h"

#import "FZQMarkViewController.h"

#import "FZQLoginPromptViewController.h"

static CGFloat const buttonW = 100;

@interface FollowVC ()<UIScrollViewDelegate>

/** 标题滚动屏 */
@property (nonatomic, weak) UIScrollView *titleView;
/** 内容滚动屏 */
@property (nonatomic, weak) UIScrollView *contentView;
/** 底部滚动条 */
@property (nonatomic, strong) UIView *titleButtomView;
/** 选中按钮 */
@property (nonatomic, weak) UIButton *selectedBtn;
/** 按钮数组 */
@property (nonatomic, strong) NSMutableArray <UIButton *> *titleItems;


@end


@implementation FollowVC
#pragma mark - lazy load
-(NSMutableArray *)titleItems
{
    if (_titleItems == nil) {
        //创建数组
        _titleItems = [NSMutableArray arrayWithCapacity:self.childViewControllers.count];
    }
    return _titleItems;
}

#pragma mark - life cycle
-(void)viewDidLoad {
    [super viewDidLoad];
    
    /** 添加所有子控制器 */
    [self setupAllChildControllers];
    
    /** 标题滚动栏 */
    [self setupTitleView];
    
    /** 内容滚动控件 */
    [self setupcontentView];
    
    /** 设置标题控件按钮 */
    [self setupTitleBtn];
    
    /** 添加标题滚动栏底部条 */
    [self setupTitleButtomView];
    
    //默认第一个按钮选中
    [self selectBtn:self.titleItems[0]];
    
    //默认添加第一个控件到contentView中
    [self addView_contentView:0];
    
    //设置自动调整边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //隐藏导航条
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //判断是否已经登录
    if ([USER_DEFAULT objectForKey:@"login"] == nil) {
    
    //遍历滚动屏子控件
    for (UIViewController *vc in self.contentView.subviews) {
        
        //将登录提示子控件移除
        if([vc isKindOfClass:[FZQLoginPromptViewController class]] == YES)[vc.view removeFromSuperview];
        }
    }
}

#pragma mark - setup
/** 添加所有子控制器 */
-(void)setupAllChildControllers
{   
    //追番
    UIViewController *vc1 = [[FZQChaseProgramViewController alloc]init];
    vc1.title = @"追番";
    [self addChildViewController:vc1];
    
    //动态
    UIViewController *vc2 = [[FZQDynamicViewController alloc]init];
    vc2.title = @"动态";
    [self addChildViewController:vc2];
    
    //标签
    UIViewController *vc3 = [[FZQMarkViewController alloc]init];
    vc3.title = @"标签";
    [self addChildViewController:vc3];

}

/** 标题滚动栏 */
-(void)setupTitleView
{  
    //设置滚动栏y值
    CGFloat y = (self.navigationController.navigationBarHidden)?0 : 20;

    //生成标题滚动栏及设置frame
    UIScrollView *titleView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, NavigationBarH)];
    
    //设置成员属性
    self.titleView = titleView;
    
    //添加到视图中
    [self.view addSubview:titleView];
    
//    //设置背景颜色
//    titleView.backgroundColor = BACKGROUND_COLOR;
    
    //设置滚动范围
    titleView.contentSize = CGSizeMake(buttonW * self.childViewControllers.count, 0);
    
    //设置分页
    titleView.pagingEnabled = YES;
}

/** 添加标题滚动栏底部条 */
-(void)setupTitleButtomView
{   
    //添加控件
    UIView *titleButtomView = [[UIView alloc]init];
    
    //控件背景颜色
    titleButtomView.backgroundColor = HPColorPink;
    
    //定义底部滚动条高度
    CGFloat titleButtomViewH = 3;
    
    //设置尺寸
    titleButtomView.frame = CGRectMake(self.titleItems[0].x, CGRectGetMaxY(self.titleItems[0].frame) - titleButtomViewH, buttonW, titleButtomViewH);

    //添加到标题滚动栏中
    [self.titleView addSubview:titleButtomView];
    
    //设置成员属性
    self.titleButtomView = titleButtomView;
}

/** 设置标题控件按钮 */
-(void)setupTitleBtn
{   
    //获取子控制数量
    NSInteger count = self.childViewControllers.count;
    
    //设置位置、尺寸参数
    CGFloat btnX = (SCREEN_WIDTH - buttonW * count) / 2;
    CGFloat btnY = 0;
    CGFloat btnW = buttonW;
    CGFloat btnH = NavigationBarH;
    
    //添加按钮
    for (NSInteger i = 0; i < count; i++) {
        
        //生成按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //设置按钮内容
        [btn setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        
        //设置按钮颜色
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        //添加按钮监听方法
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //设置按钮位置和尺寸
        btnX = (i==0)? btnX : (btnX + btnW);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
        //将按钮添加到标题滚动栏中
        [self.titleView addSubview:btn];
        
        //将按钮添加到按钮数组中
        [self.titleItems addObject:btn];
    }
}

/** 内容滚动控件 */
-(void)setupcontentView
{   
    //设置y值和高度
    CGFloat y = CGRectGetMaxY(self.titleView.frame);
    CGFloat h = (self.tabBarController)?(SCREEN_HEIGHT - y - TABBAR_HEIGHT): (SCREEN_HEIGHT - y);
    
    //生成标题滚动栏
    UIScrollView *contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, y, SCREEN_WIDTH,h)];
    
    //设置成员属性
    self.contentView = contentView;
    
    //添加到视图中
    [self.view addSubview:contentView];
    
    //设置滚动范围
    contentView.contentSize = CGSizeMake(SCREEN_WIDTH * self.childViewControllers.count, 0);
    
    //设置分页
    contentView.pagingEnabled = YES;
    
    //设置代理
    contentView.delegate = self;
    
    //隐藏底部滚动条
    contentView.showsHorizontalScrollIndicator = NO;
}

#pragma mark - observe
/* 按钮点击 */
- (void)btnClick:(UIButton *)btn
{
    //获取角标
    NSInteger index = [self.titleItems indexOfObject:btn];
    
    //设置选中按钮
    [self selectBtn:btn];
    
    //添加控件
    [self addView_contentView:index];
    
    //偏移
    //内容滚动屏
    [self.contentView setContentOffset:CGPointMake(index * SCREEN_WIDTH, 0)];
    //底部滚动条
    self.titleButtomView.transform = CGAffineTransformMakeTranslation(index * buttonW, 0);
}

#pragma mark - methods
/* 选择按钮 */
- (void)selectBtn:(UIButton *)btn
{
    //设置原按钮颜色为黑色
    [self.selectedBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    //设置按钮选中
    [btn setTitleColor:HPColorPink forState:UIControlStateNormal];
    
    //记录选中按钮
    self.selectedBtn = btn;
}

/* 添加控件 */
- (void)addView_contentView:(NSInteger )index
{
    //获取登录信息
//    NSString *login = [USER_DEFAULT objectForKey:@"login"];
    
    //生成控制器指针
    UIViewController *vc = nil;

    //根据是否登录选择对应的控制器
//    vc = (login == nil)? [[FZQLoginPromptViewController alloc]init] : self.childViewControllers[index];
    vc = self.childViewControllers[index];
    
    //判断是否已经将vc.view添加到contentScrollView中，已经添加则return
    if(vc.view.superview != nil)return;
    
    //设置控制器view的frame
    vc.view.frame = CGRectMake(index * SCREEN_WIDTH, 0, self.contentView.width, self.contentView.height);
    
    //添加到contentView中
    [self.contentView addSubview:vc.view];
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /****** 计算偏移页数 *****/
    NSInteger leftIndex = scrollView.contentOffset.x / SCREEN_WIDTH;
    NSInteger rightIndex = (leftIndex + 1 >= self.titleItems.count)? leftIndex : (leftIndex + 1);
    
    
    /****** 添加控件 *****/
    [self addView_contentView:rightIndex];
    

    /**** 设置标题底部条偏移 ***/
    self.titleButtomView.x = self.titleItems[0].x + scrollView.contentOffset.x * self.titleButtomView.width / SCREEN_WIDTH;
    
    /**** 设置按钮颜色 ***/
    
    //计算偏移
    CGFloat offset = scrollView.contentOffset.x / SCREEN_WIDTH - leftIndex;
    offset = (offset < 0)? 0 : offset;//最小偏移
    
    //若两角标一致，则是最右边页面继续往右拖动的情况，不需要再设置渐变颜色
    if (leftIndex == rightIndex) return;
    
    //左边按钮
    UIButton *btnLeft = self.titleItems[leftIndex];
    UIColor *leftColor = RGBCOLOR(((1 - offset) * (212.0 - 135) + 135), ((1 - offset) * (71.0 - 135) + 135), ((1 - offset) * (114.0 - 135) + 135));
    [btnLeft setTitleColor:leftColor forState:UIControlStateNormal];
    
    //右边按钮
    UIButton *btnRight = self.titleItems[rightIndex];
    UIColor *rightColor = RGBCOLOR((offset * (212.0 - 135) + 135), (offset * (71.0 - 135) + 135), (offset * (114.0 - 135) + 135));
    [btnRight setTitleColor:rightColor forState:UIControlStateNormal];
}

//停止拖曳的时候使用--当前场景这个可以不实现。因为对选中按钮没有太准确要求
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //计算偏移页数
    NSInteger page = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    //更新标题按钮
    [self selectBtn:self.titleItems[page]];

}

//停止滚动时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //计算偏移页数
    NSInteger page = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    //更新标题按钮
    [self selectBtn:self.titleItems[page]];
}
@end
